package com.study.spark.scala.chapter07

import scala.io.{BufferedSource, Source}

object Scala05_Collection_1{
  def main(args: Array[String]): Unit = {
    // TODO 集合 - 功能函数 - WordCount案列 - 优化
    // 函数式编程，不需要返回值，一种迭代的感觉

    // TODO 1.按行读取文件，获取原始数据
    val source: BufferedSource = Source.fromFile("output/test.txt")
    val lines = source.getLines().toArray
    source.close()

    val wordcount =
      lines
          .flatMap(_.split(" "))
          .groupBy(str => str)
          .mapValues(_.size)

    println(wordcount)//Map(Hello -> 2, Spark -> 1, Scala -> 1)

  }
}
